<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class PayorderModel extends Model
{
    // 确定链接表名
    protected $name = 'payorder';

    public function getTypeAttr($value, $data){
        $status = ['Balance'=>'余额支付','JSAPI'=>'微信支付'];
        return $status[$data['trade_type']];
    }
    public function memberModel(){
        return $this->belongsTo('MemberModel', 'memb_id', 'uid');
    }
    
    public function getStatusAttr($value){
        $status = ['未支付','已支付'];
        return $status[$value];
    }

    /**
     * 根据搜索条件获取订单列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getUsersByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的订单数量
     * @param $where
     */
    public function getAllUsers($where)
    {
        return $this->where($where)->count();
    }


    /**
     * 编辑
     * @param $param
     */
    public function editUser($param)
    {
        try{
            $result =  $this->save($param, ['orde_id' => $param['id']]);
            return msg(1, url('order/index'), '编辑订单成功');
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    /**
     * 删除订单
     * @param $id
     */
    public function del($id)
    {
        try{

            $this->where('orde_id', $id)->delete();
            return msg(1, '', '删除订单成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }


    /**
     * 更新订单状态
     * @param array $param
     */
    public function updateStatus($param = [], $uid)
    {
        try{

            $this->where('id', $uid)->update($param);
            return msg(1, '', 'ok');
        }catch (\Exception $e){

            return msg(-1, '', $e->getMessage());
        }
    }

}
